package com.normal.utils.sp

import android.util.Log
import com.hnly.djdl.BuildConfig
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 *
 *@author jiang create by 2022/11/9 20 26
 */
class StringProperty(private val key: String, private val def: String = "") :
    ReadWriteProperty<SpData, String> {
    private val TAG = "MMKV【存储String】=="
    var propertyValue = ""
    override fun getValue(thisRef: SpData, property: KProperty<*>): String {
        return propertyValue.ifEmpty {
            propertyValue = thisRef.mmkv.decodeString(key, def) ?: def
            propertyValue
        }
    }

    override fun setValue(thisRef: SpData, property: KProperty<*>, value: String) {
        propertyValue = value
        if (BuildConfig.DEBUG) {
            Log.i("StringProperty", "存储=${key}=$value")
        }
        thisRef.mmkv.encode(key, value)
    }
}